package com.autotest.nore.controller;

import com.alibaba.fastjson2.JSONObject;
import com.autotest.nore.common.constants.ResponseCode;
import com.autotest.nore.common.exception.TokenEmptyException;
import com.autotest.nore.common.utils.PageUtils;
import com.autotest.nore.common.utils.Pager;
import com.autotest.nore.dto.CaseDTO;
import com.autotest.nore.request.ApiParamRuleRequest;
import com.autotest.nore.request.CaseRequest;
import com.autotest.nore.request.QueryRequest;
import com.autotest.nore.response.TemplateResponse;
import com.autotest.nore.service.CaseGenerateService;
import com.autotest.nore.service.CaseService;
import com.autotest.nore.service.ReportService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/autotest/case")
public class CaseController {

    @Autowired
    private CaseService caseService;

    @Autowired
    private ReportService reportService;

    @Autowired
    private CaseGenerateService caseGenerateService;

    @PostMapping("/saveCaseVariable")
    public void saveCaseVariable(@RequestBody CaseRequest caseRequest){
        String caseVariables=caseRequest.getCaseVariables().toJSONString();
        String caseId=caseRequest.getId();
        caseService.updateCaseVariables(caseVariables, caseId);
    }

    @PostMapping("/save")
    public void saveCase(@RequestBody CaseRequest caseRequest, HttpServletRequest request) {
        HttpSession session = request.getSession(false); // 获取会话，如果不存在则返回null
        if (session == null || session.getAttribute("userName") == null) {
            throw new TokenEmptyException("用户未登录，请重新登录");
        }
        String userName = session.getAttribute("userName").toString();
        caseRequest.setUpdateUser(userName);
        caseService.saveCase(caseRequest);
    }


    @PostMapping("/save2")
    public String saveCase2(@RequestBody CaseRequest caseRequest, HttpServletRequest request) {
        HttpSession session = request.getSession(false); // 获取会话，如果不存在则返回null
        if (session == null || session.getAttribute("userName") == null) {
            throw new TokenEmptyException("用户未登录，请重新登录");
        }
        String userName = session.getAttribute("userName").toString();
        caseRequest.setUpdateUser(userName);
        return caseService.saveCase2(caseRequest);
    }

    @PostMapping("/delete")
    public void deleteCase(@RequestBody CaseRequest request) {
        caseService.deleteCase(request);
    }

    @GetMapping("/detail/{caseId}")
    public CaseDTO getCaseDetail2(@PathVariable String caseId){
        return caseService.getCaseDetail2(caseId);
    }


    @GetMapping("/system/{caseId}")
    public String getCaseSystem(@PathVariable String caseId){
        return caseService.getCaseSystem(caseId);
    }

    @PostMapping("/list/{goPage}/{pageSize}")
    public Pager<List<CaseDTO>> getCaseList(@PathVariable int goPage, @PathVariable int pageSize,
                                           @RequestBody QueryRequest request) {
        // 使用 PageHelper 进行分页
        Page<Object> page = PageHelper.startPage(goPage, pageSize, true);
        List<CaseDTO> caseList = caseService.getCaseList(request);
        // 将分页信息封装到 Pager 对象中
        PageInfo<CaseDTO> pageInfo = new PageInfo<>(caseList);
        return PageUtils.setPageInfo(page, caseList);
    }


    @GetMapping("/api/report/{apiId}")
    public JSONObject getLastApiReport(@PathVariable String apiId){
        return reportService.getLastApiReport(apiId);
    }

    @PostMapping("/auto/generate")
    public void generateCase(@RequestBody ApiParamRuleRequest ruleRequest, HttpServletRequest request){
        HttpSession session = request.getSession(false); // 获取会话，如果不存在则返回null
        if (session == null || session.getAttribute("userName") == null) {
            throw new TokenEmptyException("用户未登录，请重新登录");
        }
        String userName = session.getAttribute("userName").toString();
        ruleRequest.setCreateUser(userName);
        caseGenerateService.generateCase(ruleRequest);
    }

}
